package io.netty.client.config;

import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@Setter
@Configuration
@ConfigurationProperties(prefix = "thread-pool")
@Slf4j
@ToString
public class ThreadPoolConfig {

    private Integer coreSize;
    private Integer maxSize;
    private Integer keepAliveSeconds;
    private Integer queueCapacity;

    @Bean("taskExecutor")
    public Executor newThreadPoolExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(coreSize);
        executor.setMaxPoolSize(maxSize);
        executor.setKeepAliveSeconds(keepAliveSeconds);
        executor.setQueueCapacity(queueCapacity);
        executor.setThreadNamePrefix("taskExecutor---");
        log.info("ThreadPool-taskExecutor init success！{}", this);
        return executor;
    }

}
